//
//  UpdateNickName.swift
//  TrafficViolation
//
//  Created by eaglelive on 2017/4/23.
//  Copyright © 2017年 eaglelive. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import SwiftyJSON
class UpdateNickName: BaseController {
    var personal:PersonalModel!//个人信息
    var nickName:UITextField!//用户昵称
    override func viewDidLoad(){
        super.viewDidLoad()
        setupNavBar(title: "昵称修改", isback: true, appearWay: 3, isright: false)//自定义导航
        
        nickName = UITextField(frame: CGRect(x:20, y:kNavigationBarH + kStatusBarH + 30, width:kScreenW-40, height:40))
        nickName.borderStyle = UITextBorderStyle.roundedRect//设置边框样式为圆角矩形
        nickName.autocapitalizationType = .none
        nickName.placeholder = personal.nickName
        //设置左边小图片
        let nameLeftView:UIView=UIView(frame: CGRect(x:0, y:0, width:30, height:40));
        let leftImage0:UIImageView=UIImageView(frame: CGRect(x:5,y:10, width:18, height:20));
        leftImage0.image=UIImage(named: "user_name");
        nameLeftView.addSubview(leftImage0)
        nickName.leftView = nameLeftView//  设置左边小图片
        nickName.leftViewMode = UITextFieldViewMode.always
        self.view.addSubview(nickName)
        
        let button:UIButton = UIButton(type:.custom)
        button.frame = CGRect(x:20, y:kNavigationBarH + kStatusBarH + 100, width:kScreenW-40, height:40)//设置登陆按钮位置和大小
        button.layer.cornerRadius = 10;
        button.backgroundColor = appMainColor
        button.showsTouchWhenHighlighted = true //
        button.adjustsImageWhenHighlighted=false ////使触摸模式下按钮也不会变暗（半透明
        button.setTitle("保存", for:.normal)
        button.addTarget(self, action: #selector(updateNickName(sender:)), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
}
extension UpdateNickName{
    func updateNickName(sender: UIButton){
        if "" == nickName.text! {
            checkAlertFun(message: "请输入昵称！")
        }else{
           updateFun()
        }
    }
    //修改昵称
    func updateFun() {
        if reachability.isReachable {
            let parameter:[String:Any] = [
                "userFiled":"nickName",
                "updateValue":nickName.text!
            ]
            Alamofire.request("\(projectUrl)/user/updateUserInfo/\(userID!)?tokenID=\(tokenID!)&tokenUserID=\(userID!)",method:.post, parameters:parameter,headers:headers).responseJSON { (response) in
                let jsonString = response.result.value!
                let jsonDate = JSON(jsonString)
                print(jsonDate)
                if jsonDate["success"] == true {
                    if(PersonalModel.changeNickName(nickName: self.nickName.text!, userID: userID as! String)){
                        print("昵称修改成功！")
                        self.dismiss(animated: true, completion: nil)
                    }
                }else{
                    self.failOperNoticeFun(code: jsonDate["rcode"].rawString()!, message: jsonDate["rdata"].rawString()!)
                }
                
            }
        }else{
            let alertController = UIAlertController(title: alertTitle, message: netNotice, preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            alertController.addAction(cancelAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
}
